package com.pda.common_easy.pages.codereplace;

import android.content.Context;
import com.apkfuns.logutils.LogUtils;
import com.pda.common_easy.beanin.ScanfBeanIn;
import com.pda.common_easy.beanout.BaseBeanOut;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class CodeReplaceModelImpl extends BaseModel implements CodeReplaceModel {
    private String tag = "CodeResetModelImpl";
    private BaseBeanOut baseBeanOut = null;

    public CodeReplaceModelImpl(Context context) {
        super(context);
    }

    @Override
    public void codeReplace(String ocode, String ncode, final CallBack callBack) {
        try {
            ScanfBeanIn scanfBeanIn = new ScanfBeanIn();
            scanfBeanIn.setAction("ChangeProductionCode");
            scanfBeanIn.setCid(SharedPreferencesManger.getCid());
            scanfBeanIn.setUserID(SharedPreferencesManger.getUid());
            scanfBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            scanfBeanIn.setOldCode(ocode);
            scanfBeanIn.setNewCode(ncode);

            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(scanfBeanIn));
            api.downloadBillDeatail(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.ontipSuccess("数码替换成功");
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (baseBeanOut != null && baseBeanOut.getErrmsg() != null) {
                                callBack.ontip(baseBeanOut.getErrmsg());
                            } else {
                                callBack.ontip(SetConfig.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            baseBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), BaseBeanOut.class);
                            if (baseBeanOut != null && baseBeanOut.getErrcode() == 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(SetConfig.connectError);
            e.printStackTrace();
        }
    }
}

